<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/26
 * Time: 10:16
 */

namespace Wechat\Controller;

/*经纪人审核*/
use Common\Controller\AdminbaseController;

class IdController extends AdminbaseController
{
    /*经纪人后台注册*/
    public function index()
    {
        $count      = M('idcard')->order('id desc')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('idcard')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k => $v){
            $list[$k]['idcard'] = explode('|',$v['idcard']);
        }

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*搜索*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('idcard')->where(['name|idnum'=>$where])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('idcard')->where(['name|idnum'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
    /*批量通过*/
    public function delall()
    {
        $post = i('post.');
        if(IS_POST) {
            foreach ($post['ids'] as $k) {
                /*获取到证件*/
                $zhengjian = M('idcard')->where(['id' => $k])->find();
                $save = [
                    'isjingjiren'=>1,
                    'iszhuce'=>1
                ];
                M('idcard')->where(['id'=>$k])->save(['check'=>1]);
                /*提升经纪人身份*/
                M('wechatuser')->where(['openid'=>$zhengjian['openid']])->save($save);
            }
            $this->redirect(U('Id/index'));
        }
    }
    /*审核失败*/
    public function fail()
    {
        $id = i('get.id');
        $zhengjian = M('idcard')->where(['id'=>$id])->find();
        if(M('wechatuser')->where(['openid'=>$zhengjian['openid']])->save(['iszhuce'=>0])){
            M('idcard')->where(['id'=>$id])->delete();
            $this->success('操作成功！');
        }else{
            $this->error('失败，未知错误！~');
        }
    }
    /*通过审核*/
    public function del()
    {
        $id = i('get.id');
        $zhengjian = M('idcard')->where(['id'=>$id])->find();
        if(M('idcard')->where(['id'=>$id])->save(['check'=>1])){
            M('wechatuser')->where(['openid'=>$zhengjian['openid']])->save(['isjingjiren'=>1,'iszhuce'=>1]);
            $this->success('成功通过审核！~');
        }else{
            $this->error('失败，未知错误！~');
        }
    }
}